import axios from "axios";
import store from '@/store'



const BASE_URL = 'http://119.3.62.20:8083';

export const requst = createdefault()
export const loadingRequst = createdefault()

loacalLoading(loadingRequst.interceptors)


function createdefault() {
    const instance = axios.create({
        baseURL: BASE_URL,
        timeout: 3000,

    })
    instance.interceptors.response.use(handelresponse, handleError)

    return instance
}


function loacalLoading(interceptors) {

    interceptors.request.use(beforeloading)
    interceptors.response.use(afterloading, loadingError)
}

function beforeloading(config) {
    store.state.loading = true
    return config
}

function afterloading(response) {
    setTimeout(() => {
        store.state.loading = false
    }, 4000)
    return response

}


function loadingError(e) {
    store.state.loading = false
    alert('请求出错！');
    throw e
}

function handelresponse(response) {
    return response.data
}

function handleError(e) {
    alert('请求出错！');
    throw e
}